package com.wuliu.system.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wuliu.common.exception.ServiceException;
import com.wuliu.common.utils.DateUtils;

import javax.annotation.Resource;

import com.wuliu.common.utils.StringUtils;
import com.wuliu.system.domain.SysAppletRoleMenu;
import com.wuliu.common.core.domain.entity.SysAppletUser;
import com.wuliu.system.mapper.SysAppletRoleMenuMapper;
import com.wuliu.system.mapper.SysAppletUserMapper;
import com.wuliu.system.service.ISysAppletRoleMenuService;
import org.springframework.stereotype.Service;
import com.wuliu.system.mapper.SysAppletRoleMapper;
import com.wuliu.system.domain.SysAppletRole;
import com.wuliu.system.service.ISysAppletRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 前端角色信息Service业务层处理
 *
 * @author realue
 * @date 2023-10-22
 */
@Service
public class SysAppletRoleServiceImpl extends ServiceImpl<SysAppletRoleMapper, SysAppletRole> implements ISysAppletRoleService {
    @Resource
    private SysAppletRoleMapper sysAppletRoleMapper;
    @Resource
    private ISysAppletRoleMenuService sysAppletRoleMenuService;
    @Resource
    private SysAppletRoleMenuMapper sysAppletRoleMenuMapper;
    @Resource
    private SysAppletUserMapper sysAppletUserMapper;

    /**
     * 查询前端角色信息
     *
     * @param roleId 前端角色信息主键
     * @return 前端角色信息
     */
    @Override
    public SysAppletRole selectSysAppletRoleByRoleId(Long roleId) {
        Long[] menuIds = {};
        List<SysAppletRoleMenu> list = sysAppletRoleMenuMapper.selectList(new LambdaQueryWrapper<SysAppletRoleMenu>().eq(SysAppletRoleMenu::getRoleId,roleId));
        if (StringUtils.isNotEmpty(list)) {
            menuIds = list.stream().map(SysAppletRoleMenu::getMenuId).toArray(Long[]::new);
        }
        SysAppletRole sysAppletRole = sysAppletRoleMapper.selectSysAppletRoleByRoleId(roleId);
        sysAppletRole.setMenuIds(menuIds);
        return sysAppletRole;
    }

    /**
     * 查询前端角色信息列表
     *
     * @param sysAppletRole 前端角色信息
     * @return 前端角色信息
     */
    @Override
    public List<SysAppletRole> selectSysAppletRoleList(SysAppletRole sysAppletRole) {
        return sysAppletRoleMapper.selectSysAppletRoleList(sysAppletRole);
    }

    /**
     * 新增前端角色信息
     *
     * @param sysAppletRole 前端角色信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSysAppletRole(SysAppletRole sysAppletRole) {
        QueryWrapper<SysAppletRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("role_name")
                .eq("role_name", sysAppletRole.getRoleName())
                .eq("sys_account", sysAppletRole.getSysAccount());
        SysAppletRole existsRole = sysAppletRoleMapper.selectOne(queryWrapper);
        if (null != existsRole) {
            throw new ServiceException("角色名称【" + sysAppletRole.getRoleName() + "】已存在");
        }
        sysAppletRole.setCreateTime(DateUtils.getNowDate());
        sysAppletRoleMapper.insertSysAppletRole(sysAppletRole);
        return batchRoleMenu(sysAppletRole);
    }

    /**
     * 修改前端角色信息
     *
     * @param sysAppletRole 前端角色信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysAppletRole(SysAppletRole sysAppletRole) {
        QueryWrapper<SysAppletRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("role_name")
                .eq("role_name", sysAppletRole.getRoleName())
                .eq("sys_account", sysAppletRole.getSysAccount())
                .ne("role_id", sysAppletRole.getRoleId());
        SysAppletRole existsRole = sysAppletRoleMapper.selectOne(queryWrapper);
        if (null != existsRole) {
            throw new ServiceException("角色名称【" + sysAppletRole.getRoleName() + "】已存在");
        }
        sysAppletRoleMenuService.deleteSysAppletRoleMenuByRoleId(sysAppletRole.getRoleId());
        sysAppletRole.setUpdateTime(DateUtils.getNowDate());
        sysAppletRoleMapper.updateSysAppletRole(sysAppletRole);
        return batchRoleMenu(sysAppletRole);
    }

    /**
     * 批量删除前端角色信息
     *
     * @param roleIds 需要删除的前端角色信息主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSysAppletRoleByRoleIds(Long[] roleIds) {
        for (Long roleId : roleIds) {
            QueryWrapper<SysAppletRole> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id", roleId);
            SysAppletRole role = sysAppletRoleMapper.selectOne(queryWrapper);
            QueryWrapper<SysAppletUser> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("role_id", roleId);
            if (sysAppletUserMapper.selectCount(userQueryWrapper) > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", role.getRoleName()));
            }
        }
        QueryWrapper<SysAppletRoleMenu> menuWrapper = new QueryWrapper<>();
        menuWrapper.in("role_id", Arrays.asList(roleIds));
        sysAppletRoleMenuMapper.delete(menuWrapper);
        return sysAppletRoleMapper.deleteSysAppletRoleByRoleIds(roleIds);
    }

    /**
     * 删除前端角色信息信息
     *
     * @param roleId 前端角色信息主键
     * @return 结果
     */
    @Override
    public int deleteSysAppletRoleByRoleId(Long roleId) {
        return sysAppletRoleMapper.deleteSysAppletRoleByRoleId(roleId);
    }

    /**
     * 批量保存
     *
     * @param sysAppletRole
     * @return
     */
    private int batchRoleMenu(SysAppletRole sysAppletRole) {
        int rows = 1;
        if (StringUtils.isNotEmpty(sysAppletRole.getMenuIds())) {
            List<SysAppletRoleMenu> list = Arrays.stream(sysAppletRole.getMenuIds()).map(t -> {
                SysAppletRoleMenu sysAppletRoleMenu = new SysAppletRoleMenu();
                sysAppletRoleMenu.setRoleId(sysAppletRole.getRoleId());
                sysAppletRoleMenu.setMenuId(t);
                return sysAppletRoleMenu;
            }).collect(Collectors.toList());
            if (sysAppletRoleMenuService.saveBatch(list)) {
                rows = list.size();
            }
        }
        return rows;
    }
}
